package com.herui.sdyu_lib.base;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;


import com.herui.sdyu_lib.base.fragment.Fragmentation;
import com.herui.sdyu_lib.base.fragment.helper.FragmentationNullException;
import com.herui.sdyu_lib.utils.LogUtils;
import com.herui.sdyu_lib.utils.ScreenUtils;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public abstract class BaseDialogFragment extends DialogFragment {

    public String TAG = "BaseFragment";
    public boolean debug = true;

    protected BaseActivity baseActivity;

    protected Fragmentation fragmentation;

    protected boolean isCreate;
    protected boolean isInitUi;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        baseActivity = (BaseActivity) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // LogUtils.i(TAG + " onCreate" + System.currentTimeMillis() % 10000);
        // 设置取消标题栏
        setStyle(STYLE_NO_TITLE, 0);
        isCreate = false;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        fragmentation = baseActivity.getFragmentation();
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        LogUtils.i(TAG + " onCreateView");
        View view = inflater.inflate(getLayout(), container, false);
        isCreate = true;
        isInitUi = false;
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        if (!isInitUi)
            initParam();
        isInitUi = true;
        DisplayMetrics metric = baseActivity.getResources().getDisplayMetrics();
        getDialog().getWindow().setLayout((int) (metric.widthPixels * 0.6f),
                (int) (metric.heightPixels * 0.9f));
//        initLayoutParams();
    }

    protected void initLayoutParams() {
        DisplayMetrics dm = ScreenUtils.getDisplayMetrics(baseActivity);

        final WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = (int) (dm.heightPixels * 0.6f);
        layoutParams.gravity = Gravity.BOTTOM;
        getDialog().getWindow().setAttributes(layoutParams);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        // LogUtils.i(TAG + " onDestroyView");
        super.onDestroyView();
        isCreate = false;
    }

    @Override
    public void onDestroy() {
        // LogUtils.i(TAG + " onDestroy");
        super.onDestroy();
    }

    public Fragment getFragment(String tag) {
        return baseActivity.getSupportFragmentManager().findFragmentByTag(tag);
    }

    public void onBackPressed() {
        getChildFragmentManager().popBackStack();
    }


    public void runOnUiThread(Runnable action) {
        if (baseActivity != null)
            baseActivity.runOnUiThread(action);
    }

    public void removeSelf(FragmentDirectionAnim anim) {

        if (getParentFragment() != null) {
            FragmentTransaction ft = getParentFragment().getChildFragmentManager().beginTransaction();
            if (anim != null)
                ft.setCustomAnimations(anim.enterAnim, anim.exitAnim, anim.popEnter, anim.popExit);
            else
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
            ft.remove(this).commitAllowingStateLoss();
            getParentFragment().getChildFragmentManager().popBackStack();
        } else {
            fragmentation.remove(this);
        }
    }

    /**
     * 替换子Fragment
     *
     * @param childContainer
     * @param childFragment
     * @param addToBack
     */
    public void replaceChildFragment(int childContainer, Fragment childFragment, boolean
            addToBack, FragmentDirectionAnim anim) {
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        if (anim != null)
            ft.setCustomAnimations(anim.enterAnim, anim.exitAnim);
        else
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.replace(childContainer, childFragment, childFragment.getClass().getName())
                .show(childFragment);
        if (addToBack) {
            ft.addToBackStack(childFragment.getClass().getName());
        }
        ft.commit();
    }

    /**
     * 添加fragment
     *
     * @param childContainer
     * @param childFragment
     * @param addToBack
     */
    public void addChildFragment(int childContainer, Fragment childFragment, boolean
            addToBack, FragmentDirectionAnim anim) {

        if (childFragment.isVisible())
            return;

        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        if (anim != null)
            ft.setCustomAnimations(anim.enterAnim, anim.exitAnim);
        else
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        Fragment topChildFragment = getTopChildFragment();
        if (topChildFragment != null) {
            if (debug)
                LogUtils.i("addChildFragment hide top: " + topChildFragment.getClass().getName());
            ft.hide(topChildFragment);
        } else if (debug)
            LogUtils.i("addChildFragment no topfragment: ");

        if (!childFragment.isAdded())
            ft.add(childContainer, childFragment, childFragment.getClass().getName())
                    .show(childFragment);
        else
            ft.show(childFragment);
        if (addToBack) {
            ft.addToBackStack(childFragment.getClass().getName());
        }
        ft.commitAllowingStateLoss();
    }

    /**
     * 添加fragment 隐藏老的fragment 保留栈
     *
     * @param constantID
     * @param from
     * @param to
     */
    public void switchBrotherChildFragment(int constantID, BaseFragment from, BaseFragment to,
                                           FragmentDirectionAnim anim) {

        if (to.isVisible())
            return;
        FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        if (anim != null)
            ft.setCustomAnimations(anim.enterAnim,
                    anim.exitAnim);
        else
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        if (from != null)
            ft.hide(from);
        if (!to.isAdded()) {
            ft.add(constantID, to, to.TAG).show(to).commitAllowingStateLoss();
        } else {
            ft.show(to).commitAllowingStateLoss();
        }
    }

    /**
     * 得到位于栈顶的子Fragment
     *
     * @return
     */
    public BaseFragment getTopChildFragment() {
        if (fragmentation == null) {
            throw new FragmentationNullException("getTopFragment()");
        }
        return fragmentation.getTopFragment(getChildFragmentManager());
    }


    public abstract int getLayout();

    public abstract void initParam();
}
